﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.BusinessLogic.Product;
using OpenSoftStore.Orm.Logic;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.Print;

namespace OpenSoftStore.BusinessLogic.Member
{
    public class MemberValueRecordLogic : ABusinessLogic
    {
        #region Fields
        public MoneyLogic MoneyBll;
        private ProductLogic productBll;
        #endregion

        public MemberValueRecordLogic()
        {
            MoneyBll = new MoneyLogic();
            productBll = new ProductLogic();
        }

        public List<T_MemberValueRecord> GetMemberValueRecords(string where = "")
        {
            return Spa.Where<T_MemberValueRecord>(where).ToList();
        }

        public DataTable GetMemberValueRecordDataTable(string where = "")
        {
            string sql = " select T_MemberValueRecord.*, " +
                         " T_Member.Name as MemberName,T_Member.CardId as MemberCardId,T_Member.TelNum as MemberTelNum " +
                         " from T_MemberValueRecord inner join T_Member on T_MemberValueRecord.MemberId = T_Member.Id ";
            sql += where;
            sql += " order by T_MemberValueRecord.AddTime desc ";
            var data = Spa.RunSqLcmdDataSet(sql);
            if (data == null) return new DataTable();
            if (data.Tables.Count == 0) return new DataTable();
            return data.Tables[0];
        }

        public List<P_ValueRecord> GetValueRecordPrint(string where = "", string orderBy = "")
        {
            string sql = " select T_MemberValueRecord.*,T_Admin.Name as AdminName, " +
                         " T_Member.Name as MemberName,T_Member.CardId as MemberCardId," +
                         " T_Shop.Name as ShopName,T_Shop.Address as ShopAddress,T_Shop.Tel as ShopTel " +
                         " from ((T_MemberValueRecord inner join T_Member on T_MemberValueRecord.MemberId = T_Member.Id) "+
                         " inner join T_Shop on T_MemberValueRecord.ShopId=T_Shop.Id) "+
                         " inner join T_Admin on T_MemberValueRecord.AdminId=T_Admin.Id "; ;
            if (!string.IsNullOrEmpty(where)) sql = sql + where;
            //sql = sql + " order by T_Bill.AddTime desc ";
            if (!string.IsNullOrEmpty(orderBy)) sql = sql + orderBy;
            var valueRecords = Spa.SelectWhere<P_ValueRecord>(sql).ToList();
            if (!valueRecords.Any()) return null;
            return valueRecords;
        }

        public int InValue(T_MemberValueRecord memberValueRecord, T_Member tMember, List<T_Borakerage> borakerages = null)
        {
            if (memberValueRecord.CashPay > 0) memberValueRecord.PayType = "现金";
            if (memberValueRecord.BankPay > 0) memberValueRecord.PayType += "银行转账/刷卡";
            memberValueRecord.AddTime = DateTime.Now;
            memberValueRecord.Id = Guid.NewGuid().ToString("D");
            memberValueRecord.NeedPay = memberValueRecord.CashPay + memberValueRecord.BankPay;
            memberValueRecord.RealPay = memberValueRecord.CashPay + memberValueRecord.BankPay;

            var member = Spa.Where<T_Member>(m => m.Id == memberValueRecord.MemberId).FirstOrDefault();
            if (member != null)
            {
                if (!string.IsNullOrEmpty(tMember.TreeId)) member.TreeId = tMember.TreeId;
                member.HaveValue += memberValueRecord.InValue;
                Spa.UpdateObject(member);

            }
            Spa.AddObject(memberValueRecord);
            int row = Spa.SaveChanges();

            var lMoney = new L_Money
            {
                TreeId = "9bf1f0f6-6e99-4604-9e1b-f10f2c87c3c2",
                TreeName = "客户充值",
                Type = "收入",
                ObjectId = memberValueRecord.Id,
                CardId = tMember.CardId,
                BankPayMoney = memberValueRecord.BankPay,
                CashPayMoney = memberValueRecord.CashPay,
                BankAccountId = memberValueRecord.BankAccountId,
                AdminId = memberValueRecord.AdminId,
                ShopId = memberValueRecord.ShopId
            };
            row += MoneyBll.AddMoeny(lMoney);
            foreach (T_Borakerage borakerage in borakerages)
            {
                borakerage.BillId = memberValueRecord.Id;
                borakerage.BroNum = borakerage.RoyaltyRate * memberValueRecord.RealPay;
                borakerage.AdminId = borakerage.AdminId;
                borakerage.Type = "充值";
                borakerage.BrokerageType = "按利润比提成";
                borakerage.AddTime = DateTime.Now;
                borakerage.SalePrice = memberValueRecord.RealPay;
                borakerage.RoyaltyRate = borakerage.RoyaltyRate;
            }
            productBll.CalculateBorakerage(borakerages);
            return row;
        }
    }
}
